package com.example.tests;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class GroupCreationTests extends TestBase {
	
	Random rnd = new Random();
		
	@DataProvider(name = "randomGroups")
	public Iterator<Object[]> generateRandomGroups(){
		List<Object[]> list = new ArrayList<Object[]>();
		for (int i = 0; i < 5; i++) {
			GroupObject group = new GroupObject()
			.setName("name"+rnd.nextInt())
			.setHeader("header"+rnd.nextInt())
			.setFooter("footer"+rnd.nextInt());
			list.add(new Object[]{group});
		} 
		return list.iterator();
	}
	
	@Test(dataProvider = "randomGroups")
	public void testValidGroupCanBeCreated(GroupObject validGroup) throws Exception {
		Set<GroupObject> oldGroupList = app.getGroupHelper().getGroups();
		//action
		app.getGroupHelper().createGroup(validGroup);
		//verification
		Set<GroupObject> newGroupList = app.getGroupHelper().getGroups();
		verifyGroupAdded(oldGroupList, validGroup, newGroupList);
	}
	
	private void verifyGroupAdded(Set<GroupObject> oldGroupList,
			GroupObject validGroup, Set<GroupObject> newGroupList) {
		assertThat(newGroupList.size(), equalTo(oldGroupList.size()+1));
		validGroup.setId("$$$");
		oldGroupList.add(validGroup);
		validGroup.setId(null);
		assertThat(newGroupList, equalTo(oldGroupList));
	}

	@Test
	public void  testGroupWithEmptyDataCanBeCreated() throws Exception {
		Set<GroupObject> oldGroupList = app.getGroupHelper().getGroups();
		GroupObject emptyDataGroup = new GroupObject()
		.setName("Name").setHeader("Header").setFooter("Footer");
		app.getGroupHelper().createGroup(emptyDataGroup);
		Set<GroupObject> newGroupList = app.getGroupHelper().getGroups();
		verifyGroupAdded(oldGroupList, emptyDataGroup, newGroupList);
	}
}
 